# -*- coding: utf-8 -*-
import scrapy
from ..items import MayaItem


class MayadotSpider(scrapy.Spider):
    name = 'mayadot'
    allowed_domains = ['mayadot.com']
    pre_url = 'http://mayadot.com/'

    def start_requests(self):
        return [scrapy.FormRequest('http://mayadot.com/logging.php?action=login',
                                   formdata={'formhash': '08d92fb2', 'referer': 'http://mayadot.com/index.php',
                                             'loginfield': 'username', 'username': 'share_net', 'password': 'night777',
                                             'cookietime': '315360000', 'questionid': '0', 'answer': '',
                                             'loginmode': '', 'styleid': '',
                                             'loginsubmit': '%CC%E1+%26%23160%3B+%BD%BB'}, callback=self.parse)]

    def parse(self, response):
        yield scrapy.Request(url='http://mayadot.com/forumdisplay.php?fid=5&page=1', callback=self.parse_url)

    def parse_url(self, response):
        tables = response.xpath('//body/center/form/div[1]/div[1]/table')
        pages = [self.pre_url + page for page in tables[1].xpath('//td[@class="f_title"]/a/@href').extract()]
        for page in pages:
            yield scrapy.Request(url=page, callback=self.parse_jpg)

    def parse_jpg(self, response):
        item = MayaItem()
        images = response.xpath('//div[@class="t_msgfont"]/img/@src').extract()
        item['img_title'] = response.xpath('//title/text()').re('(.*?) -')[0]
        item['img_urls'] = images
        yield item


